
global data_folder_final "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results_revision"

use "W:\intimate\data\match_victim_data_clean_new", clear


// Analysis 
g av_prior_emp = (ptoim1B5 + ptoim1B4 + ptoim1B3 + ptoim1B2 + ptoim1B1)/5
g av_prior_inc = (tyotuB5 + tyotuB4 + tyotuB3 + tyotuB2 + tyotuB1)/5


g av_prior_emp1 = ptoim1B1
g av_prior_inc1 = tyotuB1

local varlist sphnro ptoim1 saiprva tyotu nchild info_missing same_spouse same_munic new_child
g same_spouse0 = 1
forvalues i = 1/5 {
	g same_spouseB`i' = (sphnro0 == sphnroB`i')
}
forvalues i = 1/5 {
	g same_spouseF`i' = (sphnro0 == sphnroF`i')
}
g same_munic0 = (kunta0 == kuntaB1)
forvalues i = 1/5 {
	g same_municB`i' = (kunta0 == kuntaB`i')
}
forvalues i = 1/5 {
	g same_municF`i' = (kunta0 == kuntaF`i')
}

g new_child0 = (nchild0<nchildB1)
forvalues i = 1/5 {
	g new_childB`i' = (nchild0 < nchildB`i')
}

forvalues i = 1/5 {
	g new_childF`i' = (nchild0 < nchildF`i')
}


forvalues i = 1/5 {
    local j = 6 -`i'
    foreach thing in `varlist' {
		ren `thing'B`i' `thing'`j'
	}
}

foreach thing in `varlist' {
    ren `thing'0 `thing'6
}

forvalues i = 1/5 {
    local j = 6 +`i'
    foreach thing in `varlist' {
		ren `thing'F`i' `thing'`j'
	}
}

g group_id = _n



reshape long `varlist', i(group_id) j(time)

*Define dummy used in the eventstudies
drop year year_event
g time_ = time-6
egen time_year_cohab = group(time_ year_start_cohab)
g year = year_start_cohab + (time - 6)
gen treat= dv_couple==1

*Time displacement dummies
g dpl_5=time_==-5 & treat==1
g dpl_4=time_==-4 & treat==1
g dpl_3=time_==-3 & treat==1
g dpl_2=time_==-2 & treat==1
g dpl_1=time_==-1 & treat==1
g dpl_0=time_==0 & treat==1
gen dpl1=time_==1 & treat==1
gen dpl2=time_==2 & treat==1
gen dpl3=time_==3 & treat==1
gen dpl4=time_==4 & treat==1
gen dpl5=time_==5 & treat==1

gen treatPost= treat==1 & time_>=0
egen full_fe = group(match_id time)
global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_1 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
global fe = "full_fe year_start_cohab" 
global cluster = "match_id"

capture program drop eventStudyGraphs_victim
program define eventStudyGraphs_victim
	args a b c 
	
	quietly {
	
	preserve
	gen t = _n
	replace t = t-11 
	replace t = . if t > 5
	
	gen coef_est =. 
	gen se_est = . 

	
	
	noisily:  reghdfe `a' $dummies, absorb($fe)  cluster($cluster)	
	
	
	*Store coef_estficients
	forvalues i= 0(1)5 {
		cap	replace coef_est= _b[dpl_`i']  if t == -`i'
		cap	replace se_est =  _se[dpl_`i']  if t == -`i'
	}
		
	forvalues i= 1(1)5 {
		cap	replace coef_est = _b[dpl`i']  if t == `i'
		cap	replace se_est =  _se[dpl`i']  if t == `i'
	}

	replace t = . if missing(coef_est)

	gen uCi = coef_est + se_est*1.96
	gen lCi  = coef_est - se_est*1.96
	
	*Main regression for top guys 
	reghdfe `a'  treatPost,  absorb($fe)  cluster($cluster)
	local beta = string(_b[treatPost], "%10.3fc")
	local se = string(_se[treatPost], "%10.3fc")
	gen obs = e(N)
		
	twoway 		(rarea uCi lCi t ,color(gs10%50) lwidth(none) )  ///
		(connected coef_est t, msymbol(O)  lcolor(gs2) mcolor(gs2) lpattern(longdash_dot)  xlabel(-5 (1) 5) ylab(`b')  ///
	     yline(0, lpattern(dash) lcolor(black)) xline(0, lpattern(dash) lcolor(black)) ytitle(`c') xtitle("Time from Cohabitation") ), ///
		 graphregion(color(white)) legend(off)
	graph export "$result_folder\victim_match_`a'.pdf", replace
	restore
	}
end

eventStudyGraphs_victim "ptoim1" "-0.1(0.02)0.02" "Employment" 

eventStudyGraphs_victim "tyotu" "-2500(500)1000" "Earnings" 

eventStudyGraphs_victim "new_child" "-0.1(0.05)0.1" "Fertility" 

eventStudyGraphs_victim "same_munic" "-0.06(0.02)0.02" "Same Municipality as t=-1" 




